home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / network / ka9q / ka9q_src.arc / AUDIT.C < prev    next >
C/C++ Source or Header  |  1988-07-28  |  2KB  |  114 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "mbuf.h"
  4.  
  5. union header {
  6.     struct {
  7.         union header *ptr;
  8.         unsigned size;
  9.     } s;
  10.     long l;
  11. };
  12. /* Perform sanity checks on mbuf. Print any errors, return 0 if none,
  13.  * nonzero otherwise
  14.  */
  15. audit(bp,file,line)
  16. struct mbuf *bp;
  17. char *file;
  18. int line;
  19. {
  20.     register struct mbuf *bp1;
  21.  
  22.     for(bp1 = bp;bp1 != NULLBUF; bp1 = bp1->next)
  23.         audit_mbuf(bp1,file,line);
  24. }
  25. audit_mbuf(bp,file,line)
  26. register struct mbuf *bp;
  27. char *file;
  28. int line;
  29. {
  30.     union header *blk;
  31.     char *bufstart,*bufend;
  32.     int16 overhead = sizeof(union header) + sizeof(struct mbuf);
  33.     int16 datasize;
  34.     int errors = 0;
  35.     char *heapbot,*heaptop;
  36. #ifndef ATARI_ST
  37.     extern char _Uend;
  38.     extern int _STKRED;
  39. #endif
  40.  
  41.     if(bp == NULLBUF)
  42.         return;
  43.  
  44. #ifndef ATARI_ST
  45.     heapbot = &_Uend;
  46.     heaptop = (char *) -_STKRED;
  47. #endif
  48.  
  49.     /* Does buffer appear to be a valid malloc'ed block? */
  50.     blk = ((union header *)bp) - 1;
  51.     if(blk->s.ptr != blk){
  52.         printf("Garbage bp %lx\n",(long)bp);
  53.         errors++;
  54.     }
  55.     if((datasize = blk->s.size*sizeof(union header) - overhead) != 0){
  56.         /* mbuf has data area associated with it, verify that
  57.          * pointers are within it
  58.          */
  59.         bufstart = (char *)(bp + 1);
  60.         bufend = (char *)bufstart + datasize;
  61.         if(bp->data < bufstart){
  62.             printf("Data pointer before buffer\n");
  63.             errors++;
  64.         }
  65.         if(bp->data + bp->cnt > bufend){
  66.             printf("Data pointer + count past bounds\n");
  67.             errors++;
  68.         }
  69.     } else {
  70.         /* Dup'ed mbuf, at least check that pointers are within
  71.          * heap area
  72.         */
  73.  
  74.         if(bp->data < heapbot
  75.          || bp->data + bp->cnt > heaptop){
  76.             printf("Data outside heap\n");
  77.             errors++;
  78.         }
  79.     }
  80.     /* Now check link list pointers */
  81.     if(bp->next != NULLBUF && ((bp->next < (struct mbuf *)heapbot)
  82.          || bp->next > (struct mbuf *)heaptop)){
  83.             printf("next pointer out of limits\n");
  84.             errors++;
  85.     }
  86.     if(bp->anext != NULLBUF && ((bp->anext < (struct mbuf *)heapbot)
  87.          || bp->anext > (struct mbuf *)heaptop)){
  88.             printf("anext pointer out of limits\n");
  89.             errors++;
  90.     }
  91.     if(errors != 0){
  92.         dumpbuf(bp);
  93.         printf("PANIC: buffer audit failure in %s line %d\n",file,line);
  94.         fflush(stdout);
  95.         for(;;)
  96.             ;
  97.     }
  98.     return;
  99. }
  100. dumpbuf(bp)
  101. struct mbuf *bp;
  102. {
  103.     union header *blk;
  104.     if(bp == NULLBUF){
  105.         printf("NULL BUFFER\n");
  106.         return;
  107.     }
  108.     blk = ((union header *)bp) - 1;
  109.     printf("bp %lx tot siz %u data %lx cnt %u next %lx anext %lx\n",
  110.         (long)bp,blk->s.size * sizeof(union header),
  111.         (long)bp->data,bp->cnt,
  112.         (long)bp->next,(long)bp->anext);
  113. }
  114.